

*******************************************************************************************************************************************
* Table 4: Effects on the extensive margin of value received
* This table reports estimated treatment effect on the extensive margin of the values received by beneficiaries per month in endline one

*******************************************************************************************************************************************

use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

* Keep if surveyed or classified as ghost
keep if ss_code == "SS01" | ghost_final == 1

*Create seeded status
gen seeded_ind     = 1 if uidcount > 0 & !missing(uidcount)
replace seeded_ind = 0 if uidcount == 0  

*Create interaction variable
gen interaction = treatment * seeded_ind

svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)



local tflistp ""



loc ration "rice wheat sugar salt kero"

foreach rat of local ration {

*generate baseline    
gen bl_var = (value_`rat'_y0>0)
replace bl_var=. if missing(value_`rat'_y0)
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(value_`rat'_y0)
replace bl_var = bl_var_mean if bl_var_mi == 1

preserve
keep uid value_`rat'_jan17 value_`rat'_feb17 value_`rat'_mar17 bl_var bl_var_mi treatment strata pweight block_code ghost_final interaction
gen value_`rat'1 = value_`rat'_jan17
gen value_`rat'2 = value_`rat'_feb17
gen value_`rat'3 = value_`rat'_mar17

drop value_`rat'_jan17 value_`rat'_feb17 value_`rat'_mar17

reshape long value_`rat', i(uid) j(month)

count
scalar obs_all = 3*3960

gen treatmentXmonth=treatment*month

*Impute 0s for ghost HHs
replace value_`rat' = 0 if ghost_final == 1

*generate indicator for if received positive value
gen value_`rat'0 = (value_`rat' > 0)
replace value_`rat'0 = . if missing(value_`rat')

qui svy: mean value_`rat'0 if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)


*pooled with BL
eststo `rat'poolBL10: xi: reg value_`rat'0 treatment interaction bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

tempfile `rat'poolBL10
parmest, label saving("``rat'poolBL10'")
local tflistp "`tflistp' ``rat'poolBL10'"


restore

drop bl_var* 
}

*Total Value


count
scalar obs = 3960

*generate baseline    
gen bl_var = (value_total_y0>0)
replace bl_var=. if missing(value_total_y0)
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(value_total_y0)
replace bl_var = bl_var_mean if bl_var_mi == 1

gen seeded_missing = missing(uidcount) 

preserve
keep value_total_mar17 value_total_feb17 value_total_jan17 bl_var bl_var_mi treatment strata pweight block_code uid uidcount seeded_ind rationcardtype ss_code ghost_final b20_adr_seed_hh_0_1 interaction
gen value_total3 = value_total_mar17
gen value_total2 = value_total_feb17
gen value_total1 = value_total_jan17
drop value_total_mar17 value_total_feb17 value_total_jan17
reshape long value_total, i(uid) j(month)

count
scalar obs_all 		= 3 * 3960
scalar obs_seeded   = 3 * 2474
scalar obs_unseeded	= 3 * 695

gen treatmentXmonth=treatment*month

*Impute 0 for ghost HHs
replace value_total = 0 if ghost_final == 1

gen value_total0 = (value_total > 0)
replace value_total0 = . if missing(value_total)

qui svy: mean value_total0 if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

qui svy: mean value_total0 if treatment == 0 & seeded_ind == 1
qui estat sd
matrix Mean0_seeded = r(mean)

qui svy: mean value_total0 if treatment == 0 & seeded_ind == 0
qui estat sd
matrix Mean0_unseeded = r(mean)

qui svy: mean value_total0 if treatment == 0 & missing(seeded_ind)
qui estat sd
matrix Mean0_missing = r(mean)

*Summary stat for HHs with no Aadhaar seeded in treatment areas
svy: mean value_total0 if treatment == 1 & b20_adr_seed_hh_0_1 == 1


*Main specification
eststo totalpoolBL10: xi: reg value_total0 treatment interaction i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

* Main specification subsetted by seed status
* Seeded
eststo totalpoolBL10_seeded: xi: reg value_total0 treatment i.strata bl_var bl_var_mi [pw = pweight] if seeded_ind == 1, cluster(block_code)
estadd scalar control_mean = `=Mean0_seeded[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

* Unseeded
eststo totalpoolBL10_unseeded: xi: reg value_total0 treatment i.strata bl_var bl_var_mi [pw = pweight] if seeded_ind == 0, cluster(block_code)
estadd scalar control_mean = `=Mean0_unseeded[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

* Missing seed status
eststo totalpoolBL10_missing_seed: xi: reg value_total0 treatment i.strata bl_var bl_var_mi [pw = pweight] if missing(seeded_ind), cluster(block_code)
estadd scalar control_mean = `=Mean0_missing[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all
       
restore



preserve

 
local ration "rice wheat sugar salt kero"

 *pooled with BL
dsconcat `tflistp',subset(parm p in 1) 
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

forval i = 1/5 {
local estname: word `i' of `tflistp'
local rat: word `i' of `ration'
estimates restore `rat'poolBL10


mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue)
}

*# delimit ;
				
*** Original table
esttab totalpoolBL10 ricepoolBL10 wheatpoolBL10 sugarpoolBL10 saltpoolBL10 keropoolBL10 using  "${OutputDir}/Table4.tex",	///
	drop(_cons _Istrata* bl_var*)																							///																					
	cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) 						///
    starlevels( * 0.10 ** 0.05 *** 0.01) replace 																			///
    collabels(none) booktabs 																								///
	order (treatment interaction)																							///
	varlabels(treatment "Treatment" interaction "Treatment*Seeded", elist(treatment \addlinespace)  )						///
	stats(r2_a control_mean N percent_obs, 																					///
			labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample"								///
				) fmt(2 %12.2g %15.0fc 0))width(\hsize)																		///
	mgroups("Any Commodity" "Rice" "Wheat" "Sugar" "Salt" "Kerosene",														///
			pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) 													///
			span erepeat(\cmidrule(lr){@span}))																				///
	mlabel(none)																											///
	substitute("Standard errors in parentheses" " "																			///
				"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "											///
				"\multicolumn{7}{l}{\footnotesize  }\\" " ")		
				
*** Main table
esttab totalpoolBL10 totalpoolBL10_seeded totalpoolBL10_unseeded using  "${OutputDir}/Table4_alt.tex", 	/// 
	drop(_cons _Istrata* bl_var*)																		///
	cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) 	///
    starlevels( * 0.10 ** 0.05 *** 0.01) replace 														///
    collabels(none) booktabs 																			///
	order (treatment interaction)																					///
	varlabels(treatment "Treatment" interaction "Treatment*Seeded", elist(treatment \addlinespace)  )									///
	stats(r2_a control_mean N percent_obs, 																///
			labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample"			///
				) fmt(2 %12.2g %15.0fc 0))width(\hsize)													///
	mgroups("Any Commodity",																			///
			pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) 									///
			span erepeat(\cmidrule(lr){@span}))															///
	mlabels("Full sample" "Seeded" "Unseeded")															///
	substitute("Standard errors in parentheses" " "														///
				"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "						///
				"\multicolumn{7}{l}{\footnotesize  }\\" " ")

esttab totalpoolBL10_missing_seed using "C:\Users\tbrai\Dropbox\github_private\ABBA\exhibits\Table4_missing_seed.tex", 						/// 
	drop(_cons _Istrata* bl_var*)																		///
	cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) 	///
    starlevels( * 0.10 ** 0.05 *** 0.01) replace 														///
    collabels(none) booktabs 																			///
	order (treatment interaction)																					///
	varlabels(treatment "Treatment" interaction "Treatment*Seeded", elist(treatment \addlinespace)  )									///
	stats(r2_a control_mean N percent_obs, 																///
			labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample"			///
				) fmt(2 %12.2g %15.0fc 0))width(\hsize)													///
	mgroups("Any Commodity",																			///
			pattern(1) prefix(\multicolumn{@span}{c}{) suffix(}) 										///
			span erepeat(\cmidrule(lr){@span}))															///
	mlabels("Missing seed")																				///
	substitute("Standard errors in parentheses" " "														///
				"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "						///
				"\multicolumn{7}{l}{\footnotesize  }\\" " ")				
				
*#delimit cr 

**** Appendix table of items
esttab ricepoolBL10 wheatpoolBL10 sugarpoolBL10 saltpoolBL10 keropoolBL10  							///
		using  "${OutputDir}/TableA_X.tex", 													///
	drop(_cons _Istrata* bl_var*)																	///
	cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) ///
    starlevels( * 0.10 ** 0.05 *** 0.01) replace 													///
    collabels(none) booktabs 																		///
	order (treatment interaction)																				///
	varlabels(treatment "Treatment" interaction "Treatment*Seeded", elist(treatment \addlinespace)  )								///
	stats(r2_a control_mean N percent_obs, 															///
			labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample"		///
				) fmt(2 %12.2g %15.0fc 0))width(\hsize)												///
	mgroups("Rice" "Wheat" "Sugar" "Salt" "Kerosene",												///
			pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) 							///
			span erepeat(\cmidrule(lr){@span}))														///
	mlabel(none)																					///
	substitute("Standard errors in parentheses" " "													///
				"\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "					///
				"\multicolumn{7}{l}{\footnotesize  }\\" " ")									


restore
eststo clear
